package com.owc.common.custom.module;

import com.owc.common.custom.annotation.Encrypt;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.introspect.BasicBeanDescription;
import org.codehaus.jackson.map.ser.BeanPropertyWriter;
import org.codehaus.jackson.map.ser.BeanSerializerModifier;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author xiaom
 * @Date 2019/12/3 10:13
 * @Version 1.0.0
 * @Description <Custom processing for Encrypt annotations>
 **/
public class EncryptedSerializerModifier extends BeanSerializerModifier {

    @Override
    public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BasicBeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
        List<BeanPropertyWriter> newWriter = new ArrayList<>();
        for(BeanPropertyWriter writer : beanProperties){
            if(null == writer.getAnnotation(Encrypt.class)){
                newWriter.add(writer);
            }else{
                //Specifying serialization
                EncryptedJsonSerializer serializer = new EncryptedJsonSerializer(writer.getSerializer());
                newWriter.add(writer.withSerializer(serializer));
            }
        }
        return newWriter;
    }
}
